<?php
/**
 * Created by PhpStorm.
 * User: Thans
 * Date: 2018/9/22
 * Time: 15:08
 */

namespace thans\auth\controller;

use app\user\model\UserModel as UserModel;

class Token
{
    protected static $instance;

    /**
     * 当前请求实例
     * @var Request
     */
    protected $request;
    protected $token;
    protected $deviceType;
    protected $uid = 0;
    protected $uinfo;
    protected $loginMsg;

    /**
     * 类架构函数
     * Auth constructor.
     */
    public function __construct()
    {
        // 初始化request
        $this->request = request();
    }

    /**
     * /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return \think\Request
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }

    //获取token
    public function getToken()
    {
        if (!$this->token) {
            $this->token = $this->request->header('IG-Token') ?: cookie('IG-Token') ?: '';
        }
        return $this->token;
    }

    public function getDeviceType()
    {
        if (!$this->deviceType) {
            $this->deviceType = $this->request->header('IG-Device-Type') ?: cookie('IG-Device-Type') ?: '';
        }
        return $this->deviceType;
    }

    /**
     * 获取器 获取数据对象的值
     * @access public
     * @param  string $name 名称
     * @return mixed
     */
    public function __get($name)
    {
        return $this->getAttr($name);
    }

    public function getAttr($name)
    {
        $none = 'get' . ucwords($name);
        return $this->$name ?? $this->$none();
    }

    public function isLogin()
    {
        if (!$this->loginMsg) {
            $map['token'] = $this->getAttr('token');
            $map['device_type'] = $this->getAttr('deviceType');
            if (!$map['token'] || !$map['device_type']) {
                return false;
            }
            $this->loginMsg = db('user_token')->where($map)->find();

            if (!$this->loginMsg) {
                return false;
            }
            //验证是否过期
            if ($this->loginMsg['expire_time'] <= time()) {
                return false;
            } else {
                $this->uid = $this->loginMsg['uid'];
                return $this->loginMsg;
            }
        } else {
            return $this->loginMsg;
        }
    }

    public function getUid()
    {
        if (!$this->uid) {
            $info = $this->isLogin();
            if ($info) {
                return $info['uid'];
            } else {
                return false;
            }
        }
        return $this->uid;
    }

    public function getUinfo()
    {
        if (!$this->uinfo) {
            $login_msg = $this->getUserSelfInfo();
            if ($login_msg['code'] == '1001') {
                $this->uinfo = $login_msg['data'];
                $this->uid = $this->uinfo['id'];
                return $this->uinfo;
            }
            return false;
        }
        return $this->uinfo;
    }

    public function getUserSelfInfo()
    {

        $res = $this->isLogin();
        if (!$res) {
            return ['code' => 4001, 'msg' => '获取失败', 'data' => ''];
        }
        $user = UserModel::get($res['uid']);
        unset($user['user_pass']);
        $userModel = new UserModel();
        if (!$user['avatar']) {
            $user['avatar'] = url('/static/upload/gifs/avatar.gif', '', '', true);
        } else {
            $user['avatar'] = $user['avatar'];
            $user['avatar_url'] = url($user['avatar'], '', '', true);
        }
        $user['role'] = $userModel->userMenu($user['id']);
        $user['group'] = $userModel->getUserGroups($user['id']);
        return ['code' => 1001, 'msg' => '获取成功', 'data' => $user->toArray()];
    }
}